package cn.tedu.carehome.foundation;

import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class keliuyun {

    @Test
    void contextLoads() {
        Map<String,Integer> map=new HashMap<>();

    }

    public static String createSign(String appId, String accessToken, String secret, Map<String, String> params) {
        Long createTime = System.currentTimeMillis();
        params.put("appId", appId);
        params.put("accessToken", accessToken);
        params.put("createTime", createTime.toString());
        //cn.hutool.core.map public class MapUtil
        String original = MapUtil.sortJoin(params, "&", "=", true) + "&" + secret;
        // cn.hutool.crypto.digest.Digester
        Digester MD5 = new Digester(DigestAlgorithm.MD5);
        String sign = MD5.digestHex(original, StandardCharsets.UTF_8);

        // 此参数为请求接口参数,请根据实际业务需要编写
        String requestParams = MapUtil.sortJoin(params, "&", "=", true) + "&sign="+sign;

        return sign;
    }


    public static String createSign(String appId, String cpid, String secret ) {
        Long createTime = System.currentTimeMillis();
        Map<String, String> params = new HashMap<>();
        params.put("appId", appId);
        params.put("cpid", cpid);
        params.put("createTime", createTime.toString());
//cn.hutool.core.map public class MapUtil
// 参数需要进行排序再连接
        String original = MapUtil.sortJoin(params, "&", "=", true) + "&" + secret;
        // cn.hutool.crypto.digest.Digester
        Digester MD5 = new Digester(DigestAlgorithm.MD5);
        String sign = MD5.digestHex(original, StandardCharsets.UTF_8);

        // 此参数为请求接口参数,请根据实际业务需要编写
        String requestParams = MapUtil.sortJoin(params, "&", "=", true) + "&sign="+sign;

        return sign;
    }


}
